package com.zhu.wx.util;

import com.google.gson.Gson;
import com.zhu.wx.entity.ArticleMsg;
import com.zhu.wx.entity.RecMsg;
import com.zhu.wx.entity.ResMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 微信对象转换
 */
public class WxParseUtil {
    private static final Logger logger = LoggerFactory.getLogger(WxParseUtil.class);

    /**
     * 解析参数为对象
     * @param params
     * @return
     */
    public static RecMsg parseToRecMsg(Map<String, String> params) {
        Map<String, String> parseMap = toLowerFirstKey(params);
        Gson gson = new Gson();
        return gson.fromJson(gson.toJson(parseMap),RecMsg.class);
    }

    /**
     * 参数首字母小写(不支持嵌套)
     * @param map
     * @return
     */
    private static Map<String, String> toLowerFirstKey(Map<String, String> map) {
        Map<String, String> result = new HashMap<>();
        try {
            if (map != null && !map.isEmpty()) {
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    String key = entry.getKey();
                    String firstChar = String.valueOf(key.charAt(0));
                    key = key.replaceFirst(firstChar, firstChar.toLowerCase()).replace("_","");
                    result.put(key, String.valueOf(entry.getValue()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("=========result" + result.toString());
        return result;
    }

    /**
     * baseMap
     * @param resMsg
     * @return
     */
    private static Map<String,String> baseMap(ResMsg resMsg){
        Map<String,String> baseMap = new HashMap<>();
        baseMap.put("ToUserName",resMsg.getToUserName());
        baseMap.put("FromUserName",resMsg.getFromUserName());
        baseMap.put("MsgType",resMsg.getMsgType());
        baseMap.put("CreateTime",String.valueOf(System.currentTimeMillis()));
        return baseMap;
    }

    /**
     * 文本回复
     * @param resMsg
     * @return
     */
    public static Map<String,String> textResMap(ResMsg resMsg){
        Map<String, String> resMap = baseMap(resMsg);
        resMap.put("Content",resMsg.getContent());
        return resMap;
    }
    /**
     * 图片回复
     * @param resMsg
     * @return
     */
    public static Map<String,String> imageResMap(ResMsg resMsg) throws Exception {
        //设置图片mediaId
        Map<String, String> imgMap = new HashMap<>();
        imgMap.put("MediaId",resMsg.getMediaId());

        Map<String, String> resMap = baseMap(resMsg);
        resMap.put("Image",XmlUtil.mapToXml(imgMap));
        return resMap;
    }
    /**
     * 语音回复
     * @param resMsg
     * @return
     */
    public static Map<String,String> voiceResMap(ResMsg resMsg) throws Exception {
        //设置语音mediaId
        Map<String, String> voiceMap = new HashMap<>();
        voiceMap.put("MediaId",resMsg.getMediaId());

        Map<String, String> resMap = baseMap(resMsg);
        resMap.put("Voice",XmlUtil.mapToXml(voiceMap));
        return resMap;
    }

    /**
     * 视频回复
     * @param resMsg
     * @return
     */
    public static Map<String,String> videoResMap(ResMsg resMsg) throws Exception {
        //设置视频mediaId
        Map<String, String> videoMap = new HashMap<>();
        videoMap.put("MediaId",resMsg.getMediaId());
        videoMap.put("Title",resMsg.getTitle());
        videoMap.put("Description",resMsg.getDescription());

        Map<String, String> resMap = baseMap(resMsg);
        resMap.put("Video",XmlUtil.mapToXml(videoMap));
        return resMap;
    }
    /**
     * 音乐回复
     * @param resMsg
     * @return
     */
    public static Map<String,String> musicResMap(ResMsg resMsg) throws Exception {
        //设置音乐mediaId
        Map<String, String> musicMap = new HashMap<>();
        musicMap.put("MusicUrl",resMsg.getMusicURL());
        musicMap.put("HQMusicUrl",resMsg.gethQMusicUrl());
        musicMap.put("ThumbMediaId",resMsg.getThumbMediaId());
        musicMap.put("Title",resMsg.getTitle());
        musicMap.put("Description",resMsg.getDescription());

        Map<String, String> resMap = baseMap(resMsg);
        resMap.put("Music",XmlUtil.mapToXml(musicMap));
        return resMap;
    }
    /**
     * 图文回复
     * @param resMsg
     * @return
     */
    public static Map<String,String> articlesResMap(ResMsg resMsg) throws Exception {
        //设置图文列表
        Map<String, String> articlesMap = new HashMap<>();
        for (ArticleMsg articleMsg : resMsg.getArticleMsgList()) {
            Map<String, String> itemMap = new HashMap<>();
            itemMap.put("Url", articleMsg.getUrl());
            itemMap.put("PicUrl", articleMsg.getPicUrl());
            itemMap.put("Title", articleMsg.getTitle());
            itemMap.put("Description", articleMsg.getDescription());
            articlesMap.put("item",XmlUtil.mapToXml(articlesMap));
        }
        Map<String, String> resMap = baseMap(resMsg);
        resMap.put("Articles",XmlUtil.mapToXml(articlesMap));
        return resMap;
    }




}
